import { archService } from "@/mdesign-lib/mdesign-app.umd.js";
import { Action } from "@/mdesign-lib/mdesign-app.umd.js";
import { hgsSettingWindow, hgsExportWindow, CloseData } from "../customWindows/hgs18";
import { ActionKey } from "@/mdesign-lib/mdesign-app.umd.js";

export class HgsSettingAction extends Action {
  keyName = "hgsSetting" as ActionKey;
  desc = "hgsSetting";

  async run() {
    const projectId = app.activeProject!.id;
    const projectConfigInfo = await archService.queryProjectConfigInfo(projectId);
    let exportUrl = "";
    let simOperationUrl = "";
    if (projectConfigInfo) {
      exportUrl = projectConfigInfo.exportUrl || "";
      simOperationUrl = projectConfigInfo.simOperationUrl || "";
    }
    await hgsSettingWindow.popup({ projectId, exportUrl, simOperationUrl }, async (res: CloseData) => {
      if (res.success) {
        if (res.exportUrl && res.simOperationUrl) {
          await archService
            .saveProjectConfigInfo(projectId, res.exportUrl, res.simOperationUrl)
            .then(() => app.$notify({ type: "success", title: "成功", message: "配置成功" }));
        }
        hgsSettingWindow.abort();
        hgsSettingWindow.done();
      }
    });
  }

  onSysMenuClick() {
    this.run();
  }
}

export class HgsImportAction extends Action {
  keyName = "hgsImport" as ActionKey;
  desc = "hgsImport";

  async onApiCall() {
    const projectId = app.activeProject?.id;
    const res = await app.$confirm
      .popup({
        title: "提示",
        content: "接收客户数据",
        status: "warning",
        cancelText: "取消"
      })
      .catch(() => false);
    const isOk = !!res;
    return {
      projectId,
      isOk
    };
  }
}

export class HgsExportAction extends Action {
  keyName = "hgsExport" as ActionKey;
  desc = "hgsExport";

  async run() {
    const projectId = app.activeProject!.id;
    await hgsExportWindow.popup({ projectId }, async (res: CloseData) => {
      if (res.success) {
        await archService
          .exportTaskInfo(projectId, res.owner || "")
          .then(() => app.$notify({ type: "success", title: "成功", message: "导出成功" }));
        hgsExportWindow.abort();
        hgsExportWindow.done();
      }
    });
  }

  onSysMenuClick() {
    this.run();
  }
}
